<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="createDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择下单时间">
        </el-date-picker>
      </el-form-item>


    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          :disabled="multiple"
          size="mini"
          @click="orderShip"
          v-hasPermi="['order:list:ship']"
        >确认发货</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          :disabled="multiple"
          size="mini"
          @click="orderReceipt"
          v-hasPermi="['order:list:receipt']"
        >确认收货</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:list:remove']"
        >删除</el-button>
      </el-col>-->

    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="商品数量" align="center" prop="number" />
      <el-table-column label="订单价格" align="center" prop="orderPrice" />
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat" />

      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单人" align="center" prop="createUser" />
       <el-table-column label="订单备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="orderInfo(scope.row)"
          >订单详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:list:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table v-loading="loading" :data="drugsOrderList" @selection-change="handleSelectionChange">
        <el-table-column label="全称" :show-overflow-tooltip="true" align="center" prop="fullName" />
        <el-table-column label="简称" :show-overflow-tooltip="true" align="center" prop="shorterName" />
        <el-table-column label="商品图" align="center" prop="pimage" >
          <template slot-scope="scope">
            <img min-width="50" height="50" :src="scope.row.pimage"/>
          </template>
        </el-table-column>
        <el-table-column label="规格" :show-overflow-tooltip="true" align="center" prop="specs" />
        <el-table-column label="适应症" :show-overflow-tooltip="true" align="center" prop="adapt" />
        <el-table-column label="药品价格" align="center"  prop="price"/>
        <el-table-column label="数量" align="center"  prop="number"/>

      </el-table>
      <div slot="footer" class="dialog-footer">
        <!--<el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, getDrugsOrderList, updateOrderShip,updateOrderReceipt } from "@/api/order/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      options: [{
        value: '',
        label: '全部'
      },{
        value: '0',
        label: '已下单'
      }, {
        value: '1',
        label: '配送'
      }, {
        value: '2',
        label: '已完成'
      }],
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      orderList: [],
      //订单药品列表
      drugsOrderList:[],
      //选中数据
      selectOrderList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugsId: undefined,
        number: undefined,
        orderNo:undefined,
        userId: undefined,
        state: undefined,
        flag: undefined,
        createDate: undefined,
        updateDate: undefined,
        createUser: undefined,
        updateUser: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drugsId: [
          { required: true, message: "t_drugs表id不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态：0-已下单确认，1-配送，2-完成不能为空", trigger: "blur" }
        ],
        flag: [
          { required: true, message: "状态：0有效，1无效不能为空", trigger: "blur" }
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateDate: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        createUser: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateUser: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    stateFormat(row, column) {
      if (row.state == 0) {
        return '已下单'
      } else if(row.state == 1) {
        return '配送中'
      }else if(row.state == 2){
        return '完成'
      }
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        drugsId: undefined,
        number: undefined,
        userId: undefined,
        state: undefined,
        flag: undefined,
        createDate: undefined,
        updateDate: undefined,
        createUser: undefined,
        updateUser: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectOrderList = selection;
      this.ids = selection.map(item => item.orderNo)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    orderInfo(row) {
      this.reset();
      this.open = true;
      this.title = "订单药品";
      const orderNo = row.orderNo
      getDrugsOrderList(orderNo).then(response => {
        this.drugsOrderList = response.rows;
      });
    },
    /** 订单发货按钮操作 */
    orderShip() {
      this.reset();
      const orderNos = this.ids
      for(var i =0;i< this.selectOrderList.length;i++){
        if(this.selectOrderList[i].state != 0 ){
          this.msgSuccess("请选择未发货订单")
          return;
        }
      }
      this.$confirm('是否确认发货订单号为"' + orderNos + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return updateOrderShip(orderNos);
      }).then(() => {
        this.getList();
        this.msgSuccess("发货成功");
      }).catch(function() {});

    },
    //订单收货
    orderReceipt() {
        this.reset();
        const orderNos = this.ids
        for(var i =0;i< this.selectOrderList.length;i++){
          if(this.selectOrderList[i].state != 1 ){
            this.msgSuccess("请选择配送中的订单")
            return;
          }
        }
        this.$confirm('是否确认收货订单号为"' + orderNos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateOrderReceipt(orderNos);
        }).then(() => {
          this.getList();
          this.msgSuccess("收货成功");
        }).catch(function() {});

      },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.state != '0'){
        this.msgSuccess("选择数据不能删除");
        return;
      }
      const ids = row.orderNo || this.ids;
      this.$confirm('是否确认删除订单号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>
